// PageLyu.cpp : implementation file
//

#include "stdafx.h"
#include "stdafx.h"
#include "fxtool.h"
#include "EffectView.h"
#include "dxutil.h"
#include "d3dutil.h"
#include "d3dfont.h"
#include "D3DEnumeration.h"
#include "D3DSettings.h"
#include <AniContainer.h>
#include "fxtool.h"
#include "MainFrm.h"
#include "Effect.h"
#include <D3DModel.h>
#include "D3Daddon.h"
#include "VShadowVolume.h"
#include "ToolView2.h"
#include <BinContainer.h>
#include "dxutil.h"
#include "mss.h"

#include "fxtool.h"
#include "PageLyu.h"
#include "ToolView2.h"
#include "MainFrm.h"
#include <stdlib.h>
#include <d3d9.h>
#include <d3dx9.h>
#include <d3dx9.h>
#include <D3D9.h>

#include "dxutil.h"
#include "d3dutil.h"
#include "d3dfont.h"
#include "D3DEnumeration.h"
#include "DXUtil.h"
#include "D3DEnumeration.h"
#include "D3DSettings.h"

//#include "D3DFile.h"
#include "D3DFont.h"
#include "D3DUtil.h"
#include "DXUtil.h"
#include <AniContainer.h>
#include "ToolCamera.h"
#include <AniContainer.h>
#include "Effect.h"
#include <AniContainer.h>
#include "FXDialog.h"
#include <AniContainer.h>
#include "D3dModel.h"
#include "D3dAddon.h"
#include "VShadowVolume.h"
#include "mss.h"
#include <D3dModel.h>
#include "Trails.h"
#include "ToolApp.h"

extern SDrawElement* g_pActivityMesh;


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPageLyu property page

IMPLEMENT_DYNCREATE(CPageLyu, CPropertyPage)

CPageLyu::CPageLyu() : CPropertyPage(CPageLyu::IDD)
{
	//{{AFX_DATA_INIT(CPageLyu)
	//}}AFX_DATA_INIT
}

CPageLyu::~CPageLyu()
{
}

void CPageLyu::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPageLyu)
	DDX_Control(pDX, IDC_TRAILR, m_TR);
	DDX_Control(pDX, IDC_TRAILG, m_TG);
	DDX_Control(pDX, IDC_TRAILB, m_TB);
	DDX_Control(pDX, IDC_SMINY, m_MinY);
	DDX_Control(pDX, IDC_SMINX, m_MinX);
	DDX_Control(pDX, IDC_SMAXY, m_MaxY);
	DDX_Control(pDX, IDC_SMAXX, m_MaxX);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPageLyu, CPropertyPage)
	//{{AFX_MSG_MAP(CPageLyu)
	ON_WM_HSCROLL()
	ON_BN_CLICKED(IDC_BUTTON1, OnSaveWeapon)
	ON_WM_VSCROLL()
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPageLyu message handlers

void CPageLyu::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	// TODO: Add your message handler code here and/or call default
	char buff[256];	
	CPropertyPage::OnHScroll(nSBCode, nPos, pScrollBar);
	CScrollBar* bar1=(CScrollBar* )GetDlgItem(IDC_SMINX);
	CScrollBar* bar2=(CScrollBar* )GetDlgItem(IDC_SMINY);
	CScrollBar* bar3=(CScrollBar* )GetDlgItem(IDC_SMAXX);
	CScrollBar* bar4=(CScrollBar* )GetDlgItem(IDC_SMAXY);
	CScrollBar* barR=(CScrollBar* )GetDlgItem(IDC_TRAILR);
	CScrollBar* barG=(CScrollBar* )GetDlgItem(IDC_TRAILB);
	CScrollBar* barB=(CScrollBar* )GetDlgItem(IDC_TRAILB);
	if (bar1==pScrollBar)
	{
		int ff=m_MinX.GetPos();
		sprintf(buff,"%02f",(float)ff/10.0f);
		((AddOnContainer*)g_pActivityMesh->Weapon)->attr.pMin.x=(float)ff/10.0f;
		//Trails::MakeTrails(g_pActivityMesh,5);
		((CMainFrame*)AfxGetMainWnd())->m_D3DApp->Render3DEnvironment();
	}
	else if (bar2==pScrollBar)
	{
		int ff=m_MinY.GetPos();
		sprintf(buff,"%02f",(float)ff/10.0f);
		((AddOnContainer*)g_pActivityMesh->Weapon)->attr.pMin.y=(float)ff/10.0f;
		//Trails::MakeTrails(g_pActivityMesh,5);
		((CMainFrame*)AfxGetMainWnd())->m_D3DApp->Render3DEnvironment();
	}
	else if (bar3==pScrollBar)
	{
		int ff=m_MaxX.GetPos();
		sprintf(buff,"%02f",(float)ff/10.0f);
		((AddOnContainer*)g_pActivityMesh->Weapon)->attr.pMax.x=(float)ff/10.0f;
		//Trails::MakeTrails(g_pActivityMesh,5);
		((CMainFrame*)AfxGetMainWnd())->m_D3DApp->Render3DEnvironment();
	}
	else if (bar4==pScrollBar)
	{
		int ff=m_MaxY.GetPos();
		sprintf(buff,"%02f",(float)ff/10.0f);
		((AddOnContainer*)g_pActivityMesh->Weapon)->attr.pMax.y=(float)ff/10.0f;
		//Trails::MakeTrails(g_pActivityMesh,5);
		((CMainFrame*)AfxGetMainWnd())->m_D3DApp->Render3DEnvironment();
	}


}

BOOL CPageLyu::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
{
	// TODO: Add your specialized code here and/or call the base class
	return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}

void CPageLyu::OnSaveWeapon() 
{
	// TODO: Add your control notification handler code here
	if (!GetModel())return;
	if (!g_pActivityMesh)return;
	if (!g_pActivityMesh->Weapon)return;

	AddOnContainer* ad = (AddOnContainer*)g_pActivityMesh->Weapon;
	char attrbuff[512];
	char fbuff[256];
	sprintf(fbuff,"C:\\YBDATA\\mesh\\%s",ad->fname);
	DWORD head=0xffffffff;
	DWORD readbyte;
	AddOnContainer *tt;
	VOID* pVertices;
	BYTE *pVert;
	VOID* pIdices;
	WORD *pId;
	int nvert=0,nface=0;
	char tbuff[256];
	char buff[50];
	HANDLE file;
	file = CreateFile( fbuff, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, 
		OPEN_EXISTING, 0, NULL );
	if (INVALID_HANDLE_VALUE == file )
		MessageBox(fbuff,NULL,MB_OK);

	WriteFile(file,&head,4,&readbyte,NULL);
	memset(buff,0,50);
	sprintf(buff,"YB_Online Mesh File v1.0");
	WriteFile(file,buff,30,&readbyte,0);
	memset(attrbuff,0,512);
	memcpy(attrbuff,&ad->attr,sizeof(MESHATTR));
	WriteFile(file,attrbuff,512,&readbyte,NULL);

	WriteFile(file,&ad->mat,sizeof(D3DMATERIAL9),&readbyte,NULL);
	memset(buff,0,50);
//	WriteFile(file,ad->texname,40,&readbyte,NULL);

	nvert=ad->mesh->GetNumVertices();
	nface=ad->mesh->GetNumFaces();
	WriteFile(file,&nvert,4,&readbyte,NULL);
	WriteFile(file,&nface,4,&readbyte,NULL);

	DWORD   cbVertexSize=D3DXGetFVFVertexSize(ad->mesh->GetFVF());
	ad->mesh->LockVertexBuffer(NULL,(VOID**)&pVertices);
	ad->mesh->LockIndexBuffer(NULL,(VOID**)&pIdices);

	VOID* attbuff;
	DWORD * rattbuff;
	pVert=(BYTE *)pVertices;
	pId=(WORD *)pIdices;
	WriteFile(file,pVert,sizeof(BINVERTEX)*nvert,&readbyte,NULL);
	WriteFile(file,pId,sizeof(WORD)*nface*3,&readbyte,NULL);
	ad->mesh->UnlockVertexBuffer();
	ad->mesh->UnlockIndexBuffer();
	CloseHandle(file);
}

int CPageLyu::DoModal() 
{
	// TODO: Add your specialized code here and/or call the base class
	
	
	return CPropertyPage::DoModal();
}

BOOL CPageLyu::OnSetActive() 
{
	if (!GetModel() || !g_pActivityMesh || !g_pActivityMesh->Weapon)return CPropertyPage::OnSetActive();
	// TODO: Add your specialized code here and/or call the base class
	m_MinX.SetRange(-25,25,0);
	m_MinX.SetPos(     ( ((AddOnContainer *)g_pActivityMesh->Weapon)->attr.pMin.x)*10);
	m_MinY.SetRange(-5,100,0);
	m_MinY.SetPos(  ( ((AddOnContainer *)g_pActivityMesh->Weapon)->attr.pMin.y)*10);
	m_MaxX.SetRange(-25,25,0);
	m_MaxX.SetPos( (  ((AddOnContainer *)g_pActivityMesh->Weapon)->attr.pMax.x)*10);
	m_MaxY.SetRange(0,130,0);
	m_MaxY.SetPos(  ( ((AddOnContainer *)g_pActivityMesh->Weapon)->attr.pMax.y)*10);
	BYTE *rr=(BYTE *)&((AddOnContainer *)g_pActivityMesh->Weapon)->attr.trailcolor;
	m_TR.SetRange(0,255,0);
	m_TR.SetPos(*(rr+2));
	m_TG.SetRange(0,255,0);
	m_TG.SetPos(*(rr+1));
	m_TB.SetRange(0,255,0);
	m_TB.SetPos(*(rr));
	((CMainFrame*)AfxGetMainWnd())->PageLyu=this;

	return CPropertyPage::OnSetActive();
}

void CPageLyu::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	// TODO: Add your message handler code here and/or call default
	
	CPropertyPage::OnVScroll(nSBCode, nPos, pScrollBar);
	CScrollBar* barR=(CScrollBar* )GetDlgItem(IDC_TRAILR);
	CScrollBar* barG=(CScrollBar* )GetDlgItem(IDC_TRAILG);
	CScrollBar* barB=(CScrollBar* )GetDlgItem(IDC_TRAILB);
	if (barR==pScrollBar)
	{
		int ff=m_TR.GetPos();
		BYTE *rr=(BYTE *)&((AddOnContainer *)g_pActivityMesh->Weapon)->attr.trailcolor;
		*(rr+2)=ff;
		//Trails::MakeTrails(g_pActivityMesh,5);
		((CMainFrame*)AfxGetMainWnd())->m_D3DApp->Render3DEnvironment();
	}
	else if (barG==pScrollBar)
	{
		int ff=m_TG.GetPos();
		BYTE *rr=(BYTE *)&((AddOnContainer *)g_pActivityMesh->Weapon)->attr.trailcolor;
		*(rr+1)=ff;
//		Trails::MakeTrails(g_pActivityMesh,5);
		((CMainFrame*)AfxGetMainWnd())->m_D3DApp->Render3DEnvironment();
	}
	else if (barB==pScrollBar)
	{
		int ff=m_TB.GetPos();
		BYTE *rr=(BYTE *)&((AddOnContainer *)g_pActivityMesh->Weapon)->attr.trailcolor;
		*(rr)=ff;
		//Trails::MakeTrails(g_pActivityMesh,5);
		((CMainFrame*)AfxGetMainWnd())->m_D3DApp->Render3DEnvironment();
	}
}
extern ANIATTR_VER2 gAniattr;

void CPageLyu::OnButton2() 
{
	char buff[80];
	int num;
	GetDlgItem(IDC_EDIT1)->GetWindowText(buff,80);
	num=atoi(buff);
	gAniattr.showarrow[0]=num;
	GetDlgItem(IDC_EDIT2)->GetWindowText(buff,80);
	num=atoi(buff);
	gAniattr.showarrow[1]=num;
	GetDlgItem(IDC_EDIT3)->GetWindowText(buff,80);
	num=atoi(buff);
	gAniattr.showarrow[2]=num;
	((CMainFrame*)AfxGetMainWnd())->m_ToolView2->OnButton12();
	// TODO: Add your control notification handler code here
	
}
